<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Jifen extends \Phpcmf\Home\Mform
{

    public function index() {
        $this->_Home_Show();
    }
	//兑换
	public function duihuan() {
	    $id = (int)\Phpcmf\Service::L('input')->get('id');
		if (!$this->uid) {
		    $rt=[
		    	'code'=>0,
		        'msg'=>'没有登陆',
		    	'data'=>'',
		    ];
		    //dr_return_data($rt['code'], $rt['msg'], $rt['data']);
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		} elseif (!$id) {
			$rt=[
				'code'=>0,
			    'msg'=>'id参数不完整',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		// 获取帖子数据
		$table = '1_quan_form_jifen';
		$jifendata =\Phpcmf\Service::M()->db->table($table)->where('id', $id)->where('shenhe',1)->get()->getRowArray();
		if (!$jifendata) {
		    $rt=[
		    	'code'=>0,
		        'msg'=>'没有这个'.SITE_SCORE.'商品',
		    	'data'=>'',
		    ];
		    return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		
		//
		$rt = \Phpcmf\Service::M()->table('1_quan_cart')->where('uid', $this->uid)->where('cid', $jifendata['cid'])->getRow();
		if(!$rt){
			$rt=[
				'code'=>0,
			    'msg'=>'您还没有加入圈子',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		
		$tablejifen = '1_quan_jifen_buy';
		$duihuandata =\Phpcmf\Service::M()->db->table($tablejifen)->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
		if($duihuandata){
			$rt=[
				'code'=>0,
			    'msg'=>'您已经兑换过',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		$shengyu = $this->member['score'] - $jifendata['jifen'];
		if($shengyu<0){
			$rt=[
				'code'=>0,
			    'msg'=>'您的'.SITE_SCORE.'不够啦',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		if($shengyu>=0){
			$notedata = $jifendata['title'];
			\Phpcmf\Service::M('member')->add_score($this->uid, -$jifendata['jifen'], $note = $notedata, $url = '', $mark = '', $count = 1);
			//添加兑换数据
			\Phpcmf\Service::M()->db->table($tablejifen)->insert(array(
			    'cid' => $id,
				'shuliang' => 1,
				'shuxing' => $jifendata['wupin'],
				'leixing' => '',
			    'uid' => $this->uid,
				'touid' => $jifendata['uid'],
				'jifen' => $jifendata['jifen'],
				'inputtime' => SYS_TIME,
			    'paiqitime' => '',
			    'jinxingtime' => '',
				'wanchengtime' => '',
				'xingming' => '',
				'shoujihaoma' => '',
				'diqu' => '',
				'dizhi' => '',
				'dizhiqueren' => '',
				'beizhu' => '',
				'dingdanzhuangtai' => '',
				'fahuozhuangtai' => '',
				'danhao' => SYS_TIME.$id,
				'wuliu' => '',
				'jindu' => '',
				'endtime' => '',
			));
			\Phpcmf\Service::M()->db->table($table)->where('id', $id)->update([
				'zongliang' => $jifendata['zongliang']+1,
			]);
			
			$rt=[
				'code'=>1,
			    'msg'=>'兑换成功',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		//dr_debug('文件名称', $shengyu);
		$rt=[
			'code'=>0,
		    'msg'=>'没有这个'.SITE_SCORE.'商品',
			'data'=>'',
		];
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}

    public function show() {
        $this->_Home_Show();
    }
	
	
	public function yiduihuan() {
	    
	    \Phpcmf\Service::V()->assign([
	    			
	    	'meta_title' => '兑换'.SITE_SCORE.'商城',
	    	'meta_keywords' => $this->get_cache('site', SITE_ID, 'config', 'SITE_KEYWORDS'),
	    	'meta_description' => $this->get_cache('site', SITE_ID, 'config', 'SITE_DESCRIPTION')
	    ]);
	    
	    \Phpcmf\Service::V()->display("mform_jifen-yiduihuan.html");
	}
	
	public function shop() {
	    
	    \Phpcmf\Service::V()->assign([
					
			'meta_title' => SITE_SCORE.'商城',
			'meta_keywords' => $this->get_cache('site', SITE_ID, 'config', 'SITE_KEYWORDS'),
			'meta_description' => $this->get_cache('site', SITE_ID, 'config', 'SITE_DESCRIPTION')
		]);
	    
	    \Phpcmf\Service::V()->display("jifen-shop.html");
	}
	
	public function yidui() {
	    
	    
	    
		\Phpcmf\Service::V()->assign([
			                
					'meta_title' => '我兑换的商品',
					'meta_keywords' => $this->get_cache('site', SITE_ID, 'config', 'SITE_KEYWORDS'),
					'meta_description' => $this->get_cache('site', SITE_ID, 'config', 'SITE_DESCRIPTION')
				]);
	    
	    \Phpcmf\Service::V()->display("jifen-yidui.html");
	}
	

    public function post() {
        $this->_Home_Post();
    }
}
